package com.tensquare.user.controller;


import com.tensquare.user.pojo.User;
import com.tensquare.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 控制器
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 登录业务
     */

    @RequestMapping(value = "/{login}",method = RequestMethod.POST)
    public Result login(@RequestBody User user){
        User userLogin = userService.login(user.getMobile(),user.getPassword());
        if (null == userLogin){
            return new Result(false,StatusCode.LOGINERROR,"登录失败");
        }
        //签发Token
        String token = jwtUtil.createJWT(userLogin.getId(),userLogin.getMobile(),"user");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("token",token);
        map.put("roles","user");
        return new Result(true,StatusCode.OK,"登录成功",map);
    }


    /**
     * 发送短信验证码
     */

    @RequestMapping(value = "/sendsms/{mobile}",method = RequestMethod.POST)
    public Result sendSms(@PathVariable String mobile){
        userService.sendSms(mobile);
        return new Result(true, StatusCode.OK,"发送成功");
    }


    /**
     * 注册业务
     */

    @RequestMapping(value = "/register/{code}",method = RequestMethod.POST)
    public Result regist(@PathVariable String code, @RequestBody User user){
        //首先我们要得到缓存中的验证码
        String checkCodeRedis = (String) redisTemplate.opsForValue().get("checkcode_"+user.getMobile());
        //结果为空
        if (checkCodeRedis.isEmpty()){
            return new Result(false,StatusCode.ERROR,"请先获取手机验证码");
        }
        //验证码不匹配
        if (!checkCodeRedis.equals(code)){
            return new Result(false,StatusCode.ERROR,"请先输入正确的获取验证码");
        }
        userService.add(user);
        return new Result(true,StatusCode.OK,"注册成功");
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@RequestBody User user,@PathVariable String id){
        user.setId(id);
        userService.update(user);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除
     */

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id){
        userService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }


    /**
     *
     * 更新好友粉丝数和用户关注数
     */
    @RequestMapping(value = "/{userid}/{friendid}/{x}",method = RequestMethod.PUT)
    public void updateFansCountAndFollowCount(@PathVariable String userid,@PathVariable String friendid,@PathVariable int x){
        System.out.println("userid:"+userid+"--friendid:"+friendid+"--x:"+x);
        userService.updateFansCountAndFollowCount(x, userid, friendid);
    }


}
